const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

function getInput(question) {
    return new Promise(resolve => {
        rl.question(question, answer => {
            resolve(answer);
        });
    });
}

async function validateInput(validate, inputPlaceholder) {
    let isValid = false;
    let input;
    while (!isValid) {
        input = await getInput(inputPlaceholder);
        isValid = validate(input);
        if (!isValid) {
            console.log('输入内容正确，请重新输入.');
        }
    }
    // rl.close();
    return input;
}

module.exports = validateInput;